package bean;

/**
 * Created by Administrator on 2018/6/25.
 */
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

public class Servlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String action=request.getServletPath();
        if(action.equals("/servlets/depend.do")){
            String value=request.getParameter("password");
            if(value.equals("123")){
                ArrayList<Bookinfo> list= Bookinfo.getbooklist();
                request.setAttribute("list", list);
                request.getRequestDispatcher("/manager.jsp").forward(request,response);
            }
            else
            {   ArrayList<Bookinfo> list= Bookinfo.getbooklist();
                request.setAttribute("list", list);
                request.getRequestDispatcher("/reader.jsp").forward(request,response);
            }
        }
        else if (action.equals("/servlets/list.do")){
            ArrayList<Bookinfo> list= Bookinfo.getbooklist();
            request.setAttribute("list", list);
            request.getRequestDispatcher("/manager.jsp").forward(request,response);
        }
        else if(action.equals("/servlets/edit.do")){
            String id =request.getParameter("id");
            Bookinfo bi= Bookinfo.getbookbyid(id);
            request.setAttribute("bi",bi);
            request.getRequestDispatcher("/edit.jsp").forward(request,response);
        }
        else if(action.equals("/servlets/update.do")){
            String id=request.getParameter("id");
            String name=request.getParameter("name");
            String author=request.getParameter("author");
            String press=request.getParameter("press");
            String date=request.getParameter("date");
            int r= Bookinfo.updatebook(id, name, author, press, date);
            if (r==1){
                request.getRequestDispatcher("/success.jsp").forward(request,response);
            }
            else{request.getRequestDispatcher("/failure.jsp").forward(request,response);
            }
        }
        else if (action.equals("/servlets/delete.do")){

            String id=request.getParameter("id");
            int r= Bookinfo.deletebook(id);
            if (r==1){
                request.getRequestDispatcher("/success.jsp").forward(request,response);
            }
            else{request.getRequestDispatcher("/failure.jsp").forward(request,response);
            }

        }
        else if (action.equals("/servlets/add.do")){

            String id=request.getParameter("id");
            String name=request.getParameter("name");
            String author=request.getParameter("author");
            String press=request.getParameter("press");
            String date=request.getParameter("date");

            int r= Bookinfo.addbook(id, name, author, press, date);
            if (r==1){
                request.getRequestDispatcher("/success.jsp").forward(request,response);
            }
            else{
                request.getRequestDispatcher("/failure.jsp").forward(request,response);
            }
        }
        else if (action.equals("/servlets/borrow.do")){
            String borrow=request.getParameter("borrow");
            if(borrow.equals("123")){
                String id=request.getParameter("id");
                int r= Bookinfo.deletebook(id);
                if (r==1){
                    request.getRequestDispatcher("/success.jsp").forward(request,response);
                }
                else{request.getRequestDispatcher("/failure.jsp").forward(request,response);
                }

            }
        }
        else if (action.equals("/servlets/check.do")){
            String id=request.getParameter("id");
            Bookinfo bi= Bookinfo.getbookbyid(id);
            request.setAttribute("bi",bi);
            request.getRequestDispatcher("/finalcheck.jsp").forward(request,response);
        }
    }
}
